package textviewer.main;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.widget.Toast;

public class DiologClickListener implements OnClickListener{
	private Context context;
	private FileIO fileio;
	private int actionID;
	private String del_file_dest;
	private GUIBookShelfChSelect guibookShelfchselect;
	private BookMark mybookmark;
	private ArrayList<String> path_list;
	private File folderToDelete;
	private GUIBookShelfBkSelect guibsbs;
	public DiologClickListener(Context context, FileIO fileio,String del_file_dest,GUIBookShelfChSelect guibookShelfchselect){
		this.context=context;
		this.fileio=fileio;
		actionID=0;
		this.del_file_dest=del_file_dest;
		this. guibookShelfchselect= guibookShelfchselect;
	}
	public DiologClickListener(Context context, BookMark mybookmark)
	{
		this.context=context;
		this.mybookmark=mybookmark;
		actionID=1;
	}
	public DiologClickListener(Context context, ArrayList<String> path_list, File folderToDelete,GUIBookShelfBkSelect guibsbs)
	{
		this.context=context;
		this.path_list=path_list;
		this.folderToDelete=folderToDelete;
		this.guibsbs= guibsbs;
		actionID=2;
	}
	public void onClick(DialogInterface dialog, int which) {
    	if(actionID==0)
    	{//context.
			boolean deleted_orNot = fileio.delete_file(del_file_dest);
			if(deleted_orNot)
			{
				Toast.makeText(context, "File Deleted", Toast.LENGTH_LONG).show();   
				Intent int_ent = new Intent(context, GUIBookShelfChSelect.class);
				int_ent.putExtra("Book_name", guibookShelfchselect.getBookName());
				//int_ent.putExtra("Book_url", Book_url);
				int_ent.putExtra("Current_Charset", guibookShelfchselect.getCharset());
				context.startActivity(int_ent);
				guibookShelfchselect.finish();
			}
			else
				Toast.makeText(context, "Error occured", Toast.LENGTH_LONG).show();  
    	}
    	else if(actionID==1)
    	{
    		mybookmark.MoveTheScrollView();
    	}
    	else if(actionID==2)
    	{
    		boolean return_result=true;
    		for(int i=0;i<path_list.size();i++)
    		{
    			FileIO.delete_file(path_list.get(i));
    		}
    		try
    		{
    			return_result=folderToDelete.delete();
    		}
    		catch(Exception e)
    		{
    			return_result=false;
    		}
    		if(return_result)
    		{
    			guibsbs.refresh_me();
    			Utility.quick_dialog("Folder deleted successfully.", context);
    		}
    		else
    			Utility.quick_dialog("Error occured while deleting the folder", context);
    		

    		
    	}
	}

}
